$(() => {
    //页面加载时,执行默认参数的search方法显示数据
    search();


    //设置查询按钮监听
    $(".actions #search").click(function () {
        search();
    });
    //新增按钮事件
    $(".actions #add").click(function () {
        // location.href = ctx + "warehouse/add";
        layer.open({
            type: 2,
            title: '商品信息详情',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['500px', '600px'],
            content: ctx + 'warehouse/add'
        });
    });

    //给删除按钮添加删除事件
    $("#delete").click(function () {
        let $checked = $("#table>tbody tr>td:first-child>:checked");
        let ids = [];
        $($checked).each(function (index, item) {
            let id = $(item).parent().next().text();
            ids.push(id);
        })
        console.log(ids)
        if (ids.length === 0) {
            layer.msg("请选择您要删除的记录");
        } else {
            layer.confirm("是否确认删除选中的记录", function (idx) {
                let url = ctx + "warehouse/delete";
                $.ajax({
                    url,
                    method: "post",
                    dataType: "json",
                    data: {
                        ids
                    }, traditional: true, success(resp) {
                        layer.msg("删除成功,共删除" + resp.rows + "条记录");

                        let pageSize = parseInt($(".paginate").data("pageSize"));
                        search(1, pageSize);
                    }, error(resp) {
                        // console.log(resp);
                        layer.msg("删除失败,请联系管理员或稍后再试!");
                    }
                })
            })
        }

    });
    //给修改按钮添加事件
    $("#edit").click(function () {
        let $checked = $("#table>tbody tr>td:first-child>:checked");
        let ids = [];
        $($checked).each(function (index, item) {
            let id = $(item).parent().next().text();
            ids.push(id);
        });
        console.log(ids);
        if (ids.length == 0) {
            layer.msg("请选中您要修改的记录");
        } else if (ids.length > 1) {
            layer.msg("一次只能修改一条数据")
        } else {
            layer.open({
                type: 2,
                title: '商品信息详情',
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['500px', '400px'],
                content: ctx + '/warehouse/edit?id=' + ids[0]
            });
            // location.href = ctx + "/warehouse/edit?id=" + ids[0];
        }
    })
    //查询本仓库商品库存信息
    $("#inventory").click(function (){
        let $checked = $("#table>tbody tr>td:first-child>:checked");
        let ids = [];
        $($checked).each(function (index, item) {
            let id = $(item).parent().next().next().text();
            ids.push(id);
        });
        console.log(ids);
        if (ids.length == 0) {
            layer.msg("请选中您要查看的仓库");
        } else if (ids.length > 1) {
            layer.msg("一次只能查询一个仓库的数据")
        } else {
            location.href = ctx + "/inventory/list?warehouseId=" + ids[0];
        }
    })
    //给分页条添加事件
    $(".paginate>div>ul").on("click", "li", function (e) {
        e.preventDefault();
        let pages = parseInt($(".paginate").data("pages"));
        let pageNo = parseInt($(".paginate").data("pageNo"));
        let pageSize = parseInt($(".paginate").data("pageSize"));

        if ($(this).is(".first")) {
            search(1, pageSize);
        } else if ($(this).is(".previous")) {
            pageNo--;
            if (pageNo < 1) {
                pageNo = 1;
            }
            search(pageNo, pageSize);
        } else if ($(this).is(".next")) {
            pageNo++;
            if (pageNo > pages) {
                pageNo = pages;
            }
            search(pageNo, pageSize);
        } else if ($(this).is(".last")) {
            search(pages, pageSize);
        } else {
            let pageNo = parseInt($(this).text());
            search(pageNo, pageSize)
        }
    });
    //设置checkAll
    $("#checkAll").click(function () {
        console.log("111")
        let checked = $(this).prop("checked");
        $("#table>tbody tr>td:first-child>:checkbox").prop("checked", checked);
    });
    $("#table>tbody").on("click", "tr>td:not(:first-child)", function () {
        let $check = $(this).closest("tr").find("td>:checkbox");
        $check.prop("checked", !$check.prop("checked"));
    });

    //设置每页条数
    $(".paginate>#pages>.choosePageSize").change(function () {
        let pageSize = $(".choosePageSize").val();
        pageSize = parseInt(pageSize);
        search(1, pageSize);
    });


});

function search(pageNo = 1, pageSize = 10) {
    let url = ctx + "warehouse/list";

    //查询条件
    let warehouseId = $("#warehouseId").val();
    let warehouseName = $("#warehouseName").val();
    let address = $("#address").val();
    let areaFrom = $("#areaFrom").val();
    let areaTo = $("#areaTo").val();
    let phone = $("#phone").val();
    console.log(areaFrom);
    console.log(areaTo);
    $.ajax({
        url,
        method: "post",
        dataType: "json",
        data: {
            pageNo,
            pageSize,
            warehouseId,
            warehouseName,
            address,
            areaFrom,
            areaTo,
            phone
        }, success(resp) {
            console.log(resp);
            let pi = resp.pi;
            $(".paginate").data("total", pi.total);
            $(".paginate").data("pageNo", pi.pageNo);
            $(".paginate").data("pageSize", pi.pageSize);
            $(".paginate").data("pages", pi.pages);


            //显示总页数和总记录数
            $(".paginate>div>.total").text(pi.total);
            $(".paginate>div>.pages").text(pi.pages);
            //显示分页条数字条
            $(".paginate>div>ul>li.number").remove();
            for (let i = pi.navigateFirst; i <= pi.navigateLast; i++) {
                let $li = $("<li class='number'><a href='#'>" + i + "</a></li>");
                if (i === pi.pageNo) {
                    $li.addClass("current");
                }
                $(".paginate>div>ul>.next").before($li);
            }

            //显示数据
            $("#table>tbody").empty();
            console.log(resp.warehouseList)
            resp.warehouseList.forEach(function (warehouse) {
                let $tr = $("<tr>");
                $tr.append("<td><input type='checkbox'></td>");
                $tr.append("<td>" + warehouse.id+"</td>");
                $tr.append("<td>" + warehouse.warehouseId + "</td>");
                $tr.append("<td>" + warehouse.warehouseName + "</td>");
                $tr.append("<td>" + warehouse.address + "</td>");
                $tr.append("<td>" + warehouse.area + "平方米</td>");
                $tr.append("<td>" + warehouse.phone + "</td>");
                // $tr.append("<td>"++"</td>");
                $("#table>tbody").append($tr);
            });
        }
    })
}